<template>
	<view class="content">
		<baidu-map class="bm-view" :center="center" :zoom="zoom" @ready="handler">
			
			
		</baidu-map>
		<view class="">
			<view class="netlist" v-for="(item,index) in networkList" :key="index">
				<view class="flx"
					@click="switchSite(item.companyId,item.companyName,item.siteId,item.siteName,item.distance,item.unit)">
					<!-- <view class="left">
						<image src="../../static/images/address.png" mode=""></image>
					</view> -->
					<view class="right">
						<view class="title">{{item.siteName}}</view>
						<view class="openTime">
							<text>正常运营</text> 开放时间: 周一至周日 8:00-17:00
						</view>
					</view>
				</view>
				<view class="line"></view>
				<view class="flx"
					@click="switchSite(item.companyId,item.companyName,item.siteId,item.siteName,item.distance,item.unit)">
					<view class="addressSmall">
						<image src="../../static/images/addressSmall.png" mode=""></image>
					</view>
					<view class="address">{{item.siteAddress}}</view>
				</view>

				<view class="flx2">
					<view class="distance"
						@click="switchSite(item.companyId,item.companyName,item.siteId,item.siteName,item.distance,item.unit)">
						距您{{item.distance}}{{item.unit}}</view>
					<view class="tel" @click="makeCall(item.mobile)">服务电话: {{item.mobile}}</view>
				</view>
			</view>
			<u-loadmore :status="status" :content-text="contentText" :iconSize="17" v-if="networkList.length" />
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	import '../../static/js/ihgAppJssdk.js'
	import vConsole from "../../static/js/vconsole.min"
	import mapIconPath from "../../static/images/localtion.png"
	// import "@/static/js/jssdk.js"
	export default {
		data() {
			return {
				status: 'loadmore',
				suo: true,
				// vConsole: new vConsole(),
				title: 'map',
				mapIcon: mapIconPath,
				scale: 9,
				latitude: '',
				longitude: '',
				covers: [{
					latitude: 39.909,
					longitude: 116.39742,
					iconPath: '../../../static/images/localtion.png'
				}, {
					latitude: 39.90,
					longitude: 116.39,
					iconPath: '../../../static/images/localtion.png'
				}],
				mapList: [{
						iconPath: '/static/images/location.png',
						width: 25, //宽
						height: 39, //高
						latitude: 30.42448,
						longitude: 120.25886,
					},
					{
						iconPath: '/static/images/location.png',
						width: 25, //宽
						height: 39, //高
						latitude: 30.22438,
						longitude: 120.15486,
					},
				],

				networkList: [],
				center: {
					lng: '',
					lat: ''
				},
				zoom: 13,
				markerPoint: {
					// lng: 116.404,
					// lat: 39.915
					lng: '',
					lat: ''
				},
				flag: false,
				lng: '',
				lat: '',
				page: 1,
				limit: 5,
				markers: [

				],
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				itemList: [],
			};
		},
		onLoad() {
			// this.initMap()
		},
		onShow() {
			setTimeout(() => {
				this.getNearbyBranchesData()
			}, 800)
		},
		methods: {
			makeCall(phone) {
				let that = this
				console.log(phone, 'phone')
				this.itemList[0] = phone
				uni.showActionSheet({
					itemList: this.itemList, //itemList字段不变
					success: function(res) {
						if (!res.cancel && res.tapIndex !== undefined) {
							let tapData = this.itemList[res.tapIndex]
							console.log(tapData, ' that.itemList[res.tapIndex]')
							uni.showModal({
								content: `拨打人号码：${phone}`,
								success: function(res) {
									console.log(res, 'res')
									console.log(that.itemList[res.tapIndex],
										' that.itemList[res.tapIndex]')
									uni.makePhoneCall({
										phoneNumber: tapData,
										success: function() {
											console.log('拨打电话成功');
										},
										fail: function() {
											console.log('拨打电话失败');
										}
									});
								}
							})

						}
					}.bind(this) // 绑定this作用域
				});
			},
			handler({
				BMap,
				map
			}) {
				let that = this
				var latitude;
				var longitude;
				ihgAppJssdk.map.getLocation({
					success: function(res) {
						console.log(res,'res')
						latitude = JSON.parse(res).latitude
						longitude = JSON.parse(res).longitude
						// that.center.lng = longitude
						// that.center.lat = latitude
						// that.markerPoint.lng = longitude
						// that.markerPoint.lat = latitude
						// // that.center.lng = 114.624593
						// // that.center.lat = 31.294312
						// // that.markerPoint.lng = 114.624593
						// // that.markerPoint.lat = 31.294312
						// // that.zoom = 13
						// that.zoom = 13
					}
				})
			},
			getNearbyBranchesData() {
				let that = this
				var lats;
				var lngs;
				ihgAppJssdk.map.getLocation({
					success: function(res) {
			
						lats = JSON.parse(res).latitude
						lngs = JSON.parse(res).longitude
						console.log(lats,lngs,'11111111')
						
						var mar = {
							lng: lngs,
							lat: lats,
						}
						request.get('/api/recycle/site/page', {
							page: that.page,
							limit: that.limit,
							lng: lngs,
							lat: lats,
							// lng: 120.314124,
							// lat: 30.413321,
						}).then(res => {
							if (res.code == 0) {
								that.networkList = res.data.list
								if (res.data.list.length == '') {
									that.status = 'nomore';
									that.suo = false
								} else if (res.data.list.length < 5) {
									that.status = 'nomore';
									that.suo = false
								} else if (res.data.list.length = 5) {
									that.status = 'loadmore';
									that.suo = true
								}
							} else if (res.code == 5) {
								let that = this
								// that.getNearbyBranchesData()
			
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none',
									duration: 2000
								})
							}
			
						})
					}
				})
			},
			
			onReachBottomGetNearbyBranchesData() {
				let that = this
				var lats;
				var lngs;
				ihgAppJssdk.map.getLocation({
					success: function(res) {
						lats = JSON.parse(res).latitude
						lngs = JSON.parse(res).longitude
						console.log(lats,lngs)
						request.get('/api/recycle/site/page', {
							page: that.page,
							limit: that.limit,
							lng: lngs,
							lat: lats,
						}).then(res => {
							if (res.code == 0) {
								that.markers = that.markers.concat(res.data.list)
								that.networkList = that.networkList.concat(res.data.list)
								if (res.data.list.length == '') {
									that.status = 'nomore';
									that.suo = false
								} else if (res.data.list.length < 5) {
									that.status = 'nomore';
									that.suo = false
								} else if (res.data.list.length = 5) {
									that.status = 'loadmore';
									that.suo = true
			
								}
							} else {
								uni.showToast({
									title: res.data.message,
									icon: 'none',
									duration: 2000
								})
							}
			
			
						})
					}
				})
			},
			switchSite(companyId, companyName, siteId, siteName, distance, unit) {

				if (unit == 'm') {
					// var unit = dista
					console.log('1111111', unit)
					console.log(distance / 1000, '1111113333')
					var recycleSiteInfo = {
						companyId: companyId,
						companyName: companyName,
						siteId: siteId,
						siteName: siteName,
						distance: distance / 1000
					}
					uni.navigateTo({
						url: '/pagesA/call-center/call-center'
					});
					uni.setStorageSync('recycleSiteInfo', recycleSiteInfo);
				}
				if (unit == 'km') {
					console.log('2222222222', unit)
					var recycleSiteInfo = {
						companyId: companyId,
						companyName: companyName,
						siteId: siteId,
						siteName: siteName,
						distance: distance
					}
					uni.navigateTo({
						url: '/pagesA/call-center/call-center'
					});
					uni.setStorageSync('recycleSiteInfo', recycleSiteInfo);
				}


			},
			
		},
		//上拉加载
		onReachBottom() {
			// this.page = 1;
			let that = this
			if (that.suo == true) { //定义个小锁，当没有数据时锁住suo
				// this.status = 'loading';
				// this.page = ++this.page;
				// this.page = 1;

				that.status = 'loading';
				that.page = ++this.page;

				setTimeout(() => {
					that.onReachBottomGetNearbyBranchesData()
				}, 1000)
				return;
			}
		},
		
	}
</script>

<style lang="less">
	.map {
		width: 100%;
		height: 480rpx;

		.bm-view {
			width: 750rpx;
			height: 480rpx;
		}
	}

	.mapList {
		margin: 24rpx 0;
	}

	.netlist {
		width: 654rpx;
		background: #FFFFFF;
		margin: 24rpx;
		padding: 24rpx;

		.flx {
			display: flex;
			align-items: center;

			.left {
				image {
					width: 82rpx;
					height: 82rpx;
					vertical-align: middle;
				}
			}

			.right {
				margin-left: 30rpx;

				.title {
					font-family: Source Han Sans SC;
					font-weight: 500;
					font-size: 36rpx;
					color: #000000;
				}

				.openTime {
					font-family: Source Han Sans SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #000000;
					margin-top: 6rpx;

					text {
						color: #FFA500;
						margin-right: 20rpx;
					}
				}
			}

			.addressSmall {
				image {
					width: 28rpx;
					height: 30rpx;
					vertical-align: middle;
				}
			}

			.address {
				font-family: Source Han Sans SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;
				line-height: 36rpx;
				margin-left: 16rpx;
			}
		}

		.line {
			width: 100%;
			height: 1rpx;
			background: #EBEBEB;
			margin: 16rpx auto;
		}

		.flx2 {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-family: Source Han Sans SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #666;
			line-height: 36rpx;
			margin-top: 10rpx;
		}
	}

	.vehicleMap {
		width: 750rpx;
		height: 600rpx;
	}

	.content {
		padding-top: 2rpx;
		background: #F1F4F9;
		padding-bottom: 2rpx;
	}
</style>